package com.bluefish.bltuan.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.msg.Message;
import com.bluefish.bltuan.service.CategoryService;

public class InfoAction extends OpenAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 281634181380063202L;

	private List<Message> messages;

	private CategoryService categoryService;

	private Category currentCity;
	
	@Resource
	private String defaultCity;
	
	@Override
	public HttpHeaders index() {
		this.messages = this.getMsgService().listQuestions(getPageIndex(LARGE_NUMBER));
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}
	
	@Override
	public HttpHeaders show() {
		this.currentCity = this.categoryService.getCityByName(this.defaultCity);
		if("question".equals(this.getId())){
			return question();
		}else if("transfer".equals(this.getId())){
			return transfer();
		}else{
			return null;
		}
	}
	
	public HttpHeaders question(){
		if(this.currentCity == null){
			this.currentCity = this.categoryService.getCityByNickName(this.getId());
		}
		this.messages = this.getMsgService().listQuestions(getPageIndex(LARGE_NUMBER),this.currentCity);
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}
	
	public HttpHeaders transfer(){
		if(this.currentCity == null){
			this.currentCity = this.categoryService.getCityByNickName(this.getId());
		}
		this.messages = this.getMsgService().listTransfers(getPageIndex(LARGE_NUMBER),this.currentCity);
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}
	
	public List<Message> getMessages() {
		return messages;
	}
	
	public void setDefaultCity(String defaultCity) {
		this.defaultCity = defaultCity;
	}
	
	public Category getCurrentCity() {
		return currentCity;
	}
	
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
}
